package com.gzhxhx.czh.myhuibaomin.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.gzhxhx.czh.myhuibaomin.Activity.WebActivity;
import com.gzhxhx.czh.myhuibaomin.R;
import com.gzhxhx.czh.myhuibaomin.adapter.ShoyegrideAdapter;
import com.gzhxhx.czh.myhuibaomin.adapter.ShoyelistAdapter;
import com.gzhxhx.czh.myhuibaomin.httpgetset.ShoyeListUtils;
import com.gzhxhx.czh.myhuibaomin.httpgetset.ShoyegrideUtils;
import com.gzhxhx.czh.myhuibaomin.utils.GlideImageLoader;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import static com.gzhxhx.czh.myhuibaomin.httpapi.HttpApi.LUNBO;
import static com.gzhxhx.czh.myhuibaomin.httpapi.HttpApi.SHOYEGRIDE;
import static com.gzhxhx.czh.myhuibaomin.httpapi.HttpApi.SHOYELIST;

@ContentView(R.layout.fragment_shoye)
public class ShoyeFragment extends Fragment{
    @ViewInject(R.id.grd_shoye_gride)
    private GridView gridView;
    @ViewInject(R.id.banner_shoye_lb)
    private Banner bannerLb;
    @ViewInject(R.id.grd_shoye_list)
    private GridView gridList;
    private List<String> images = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private List<String> url = new ArrayList<>();
    private List<ShoyegrideUtils.DataBean> list = new ArrayList<>();
    private List<ShoyeListUtils.DataBean> listlist = new ArrayList<>();
    private ShoyegrideAdapter adapter;
    private ShoyelistAdapter listadater;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this,inflater,container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        intView();
        httpLunbo();
        httpGride();
        httpgridList();

    }

    private void httpgridList() {
            OkGo.<String>get(SHOYELIST)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response.body());
                                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                                    for (int i=0;i<jsonArray.length();i++){
                                        ShoyeListUtils.DataBean dataBean = new ShoyeListUtils.DataBean();
                                        JSONObject object = jsonArray.getJSONObject(i);
                                        dataBean.setDizhi(object.optString("dizhi"));
                                        dataBean.setGuishu(object.optString("guishu"));
                                        dataBean.setId(object.optString("id"));
                                        dataBean.setImgurl(object.optString("imgurl"));
                                        dataBean.setJiage(object.optString("jiage"));
                                        dataBean.setPaixu(object.optString("paixu"));
                                        dataBean.setShijian(object.optString("shijian"));
                                        dataBean.setWenzhi(object.optString("wenzhi"));
                                        dataBean.setXiaolian(object.optString("xiaoliang"));
                                        listlist.add(dataBean);
                                    }
                                listadater.notifyDataSetChanged();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });



    }

    private void httpGride() {
        OkGo.<String>get(SHOYEGRIDE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            JSONArray jsonArray =  jsonObject.getJSONArray("data");
                            for (int i = 0;i<jsonArray.length();i++){
                                ShoyegrideUtils.DataBean dataBean = new ShoyegrideUtils.DataBean();
                                JSONObject object =  jsonArray.getJSONObject(i);
                                dataBean.setImgurl(object.optString("imgurl"));
                                dataBean.setDjjr(object.optString("djjr"));
                                dataBean.setId(object.optString("id"));
                                dataBean.setLeibie(object.optString("leibie"));
                                dataBean.setShijian(object.optString("shijian"));
                                dataBean.setToken(object.optString("token"));
                                dataBean.setWenzhi(object.optString("wenzhi"));
                                list.add(dataBean);
                            }
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    private void intView() {
        adapter = new ShoyegrideAdapter(getActivity(),list);
        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listadater  = new ShoyelistAdapter(getActivity(),listlist);
        gridList.setAdapter(listadater);
        listadater.notifyDataSetChanged();

    }

    private void httpLunbo() {
        OkGo.<String>get(LUNBO)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0;i<jsonArray.length();i++){
                                JSONObject object = jsonArray.getJSONObject(i);
                                url.add(object.optString("weburl"));
                                titles.add(object.optString("wenzhi"));
                                images.add(object.optString("imgurl"));
                            }
                        } catch (JSONException e) {
                        }
                       lunBo();
                    }
                });
    }

    private void lunBo() {
        bannerLb.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        bannerLb.setIndicatorGravity(BannerConfig.CENTER);
        bannerLb.setImageLoader(new GlideImageLoader());
        bannerLb.setImages(images);
        bannerLb.setBannerTitles(titles);
        bannerLb.setDelayTime(3000);
        bannerLb.start();
        bannerLb.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
//                Toast.makeText(getActivity(), "你点击了：" + position+"连接"+titles.get(position-1), Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("url",url.get(position-1));
                startActivity(intent);
            }
        });
    }
}
